package com.wanmait.shoppingsys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wanmait.shoppingsys.util.DbHelper;
import com.wanmait.shoppingsys.util.ShopCar;
import com.wanmait.shoppingsys.vo.Goods;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.Trade;

public class TradeDAO {

	private static TradeDAO tradeDAO;
	public static TradeDAO getDAO() {
		if(tradeDAO==null)
		{
			tradeDAO=new TradeDAO();
		}
		return tradeDAO;
	}

	
	private TradeDAO() {}
	
	private List<Trade> findbysql (String sql,List params)
	{
		ArrayList<Trade> trades =new ArrayList<Trade>();
		
		Connection conn = DbHelper.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			ps = conn.prepareStatement(sql);
			for(int i=0;params!=null&&i<params.size();i++) {
				ps.setObject(i+1, params.get(i));
			}
			rs = ps.executeQuery();
			while(rs.next()) {
				Trade trade=new Trade();
				trade.setId(rs.getInt("id"));
				trade.setOrderPrice(rs.getDouble("order_price"));
				trade.setSpecialtyId(rs.getInt("specialty_id"));
				trade.setOrderCount(rs.getInt("order_count"));
				trade.setOrderId(rs.getInt("order_id"));
				
				
				//book.setType(rs.getString("type"));
				Goods goods=new Goods();
				goods.setId(rs.getInt("id"));
				goods.setAddress(rs.getString("address"));
				goods.setIncomeName(rs.getString("income_name"));
				goods.setOrderNum(rs.getString("order_num"));
				goods.setOrderState(rs.getString("order_state"));
				goods.setPayTime(rs.getTimestamp("pay_time"));
				goods.setPhone(rs.getString("phone"));
				goods.setTotalPrice(rs.getDouble("total_price"));
				goods.setUserId(rs.getInt("user_id"));
				
				
				trade.setGoods(goods);
				trades.add(trade);


			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbHelper.close(rs, ps, conn);
		}
		return trades;
	}
	
	// 查找所有
	public List<Trade> findall() {
		
		String sql="SELECT trade_order.*,goods_order.* FROM trade_order LEFT JOIN goods_order ON trade_order.order_id=goods_order.id";
		
		return this.findbysql(sql, null);
	}
	
	//增加
	public void insert(Trade trade)
	{
		String sql="INSERT INTO trade_order (specialty_id,order_count,order_price,order_id,) VALUES(?,?,?,?)";
		ArrayList params = new ArrayList();
		params.add(trade.getSpecialtyId());
		params.add(trade.getOrderCount());
		params.add(trade.getOrderPrice());
		params.add(trade.getOrderId());
		
		DbHelper.update(sql, params);
	}
	
	//删除
	public void delete(Integer id) {
		String sql = "DELETE FROM  trade_order WHERE id=?";
		ArrayList params = new ArrayList();
		params.add(id);
		DbHelper.update(sql,params);
	}
	
	//更新
	public void updade(Trade trade)
	{
		String sql="UPDATE trade_order SET specialty_id=?,order_count=?,order_price=?,order_id=?where id=?";
		
		ArrayList params = new ArrayList();
		params.add(trade.getSpecialtyId());
		params.add(trade.getOrderCount());
		params.add(trade.getOrderPrice());
		params.add(trade.getOrderId());
		
		DbHelper.update(sql, params);
 	}
	
	//单个数量增加
	public void updateOrderCount(Integer id) {
		String sql = "UPDATE trade_order SET order_count = order_count+1 WHERE id=?";
		ArrayList params = new ArrayList();
		params.add(id);
		DbHelper.update(sql,params);
	}
	
	public void insert(ShopCar shopCar,Integer id)
	{
		String sql="INSERT INTO trade_order (specialty_id,order_count,order_price,order_id) VALUES(?,?,?,?)";
	
		for (int i = 0; i < shopCar.getSpecialtys().size(); i++) {
			
			Specialty  specialty = shopCar.getSpecialtys().get(i);
			
			
			ArrayList params = new ArrayList();
			params.add(specialty.getId());
			params.add(specialty.getCount());
			params.add(specialty.getPrice());
			params.add(id);
		
			DbHelper.update(sql, params);
			
		}
		
	}
	
}











